var LOGOUT = 407;
var NORIGHT = 403;

/**
 * 初始化
 */
$(function () {
	/**
	 * 重写EasyUI datebox 格式化函数
	 */
	$.fn.datebox.defaults.formatter = function(date) {
		var y = date.getFullYear();
	    var m = date.getMonth() + 1;
	    var d = date.getDate();
	    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
	}
	
	/**
	 * 重写EasyUI datetimebox 格式化函数
	 */
	$.fn.datetimebox.defaults.formatter = function(date){
		var y = date.getFullYear();
	    var m = date.getMonth() + 1;
	    var d = date.getDate();
	    var h = date.getHours();
	    var mn = date.getMinutes();
	    var s = date.getSeconds();
	    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d) + ' ' + (h < 10 ? ('0' + h) : h) + ':' + (mn < 10 ? ('0' + mn) : mn) + ':' + (s < 10 ? ('0' + s) : s);
	}
});

$(document).ready(function () {
	$('.menuItems li').click(function () {
		$('.menuItems li.menuItemSelected').removeClass('menuItemSelected');
		$(this).addClass('menuItemSelected');
//		APP.openMenuTab($.trim($(this).text()), $(this).attr('url'));
		APP.openMenuIframeTab($.trim($(this).text()), $(this).attr('url'));
	});
});

/**
 * 系统对象，定义系统通用方法
 */
var APP = {
	/**
	 * 打开一个菜单TAB页
	 */
	openMenuTab: function (tabName, targetUrl) {
		if ($('#mainTabDiv').tabs('exists', tabName)) {
			$('#mainTabDiv').tabs('select', tabName);
			$('#mainTabDiv').tabs('getSelected').panel('refresh');
		} else {
			$('#mainTabDiv').tabs('add', {
				title: tabName,
				href: targetUrl,
				closable: true,
                closed: true
			 });
		}
	},
	/**
	 * 打开一个菜单TAB页，用IFRAME打开
	 */
	openMenuIframeTab: function (tabName, targetUrl) {
		if ($('#mainTabDiv').tabs('exists', tabName)) {
			$('#mainTabDiv').tabs('close', tabName)
		}
		$('#mainTabDiv').tabs('add', {
			title: tabName,
			content: '<iframe scrolling="no" frameborder="0"  src="' + targetUrl + '" style="width:99%;height:99%;"></iframe>',
			closable: true,
            closed: true
		 });
	},
	/***
	 * 在iframe里面刷新当前tab
	 */
	refreshCurrTab : function() {
		var currTab = self.parent.$('#mainTabDiv').tabs('getSelected'); // 获得当前tab
		var url = $(currTab.panel('options').content).attr('src');
		self.parent.$('#mainTabDiv').tabs('update', {
			tab : currTab,
			options : {
				content : '<iframe scrolling="yes" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>',
			}
		});
	},
	/**
	 * 获取项目根URL
	 */
	getRoot: function () {
		var root = $('base').attr('href');
		return $.trim(root) == '' && parent != null ? $("base", window.parent.document).attr('href') : root;
	},
	/**
	 * 文件上传初始化
	 */
	initUpload: function (options) {
		if ($.trim(options.id) == '') {
			APP.error('请传入文件域ID, 包含#');
			return;
		}
		if ($.trim(options.url) == '') {
			APP.error('请传入要上传到的URL');
			return;
		}
		
		var realOptions = {
			swf : APP.getRoot() + '/static/js/uploadify/uploadify.swf',
	        uploader : options.url,
	        buttonText : '上传',
	        height : 20,
	        width : 60
		};
		if ($.trim(options.fileTypeExts) != '') {
			realOptions.fileTypeExts = options.fileTypeExts;
		}
		if (options.auto) {
			realOptions.auto = true;
		} else {
			realOptions.auto = false;
		}
		if (options.queueID && $.trim(options.queueID) != '') {
			realOptions.queueID = options.queueID;
		}
		if (options.multi) {
			realOptions.multi = true;
		} else {
			realOptions.multi = false;
		}
		if (options.getFormDataFun) {
			realOptions.onUploadStart = function (file) {
				var formData = options.getFormDataFun();
				$(options.id).uploadify('settings', 'formData', formData);
			}
		}
		if (options.callback) {
			realOptions.onUploadSuccess = function (file, data, response) {
				APP.message('上传成功');
				options.callback(eval('(' + data + ')'));
			}
		}
		$(options.id).uploadify(realOptions);
	},
	/**
	 * 右下角提示信息
	 */
	message: function (msg) {
		$.messager.show({
            title: '提示',
            msg: msg,
            timeout: 3000,
            showType: 'slide'
        });
	},
	/**
	 * 错误提示
	 */
	error: function (msg) {
		 $.messager.alert('错误', msg, 'error');
	},
	/**
	 * 警告提示
	 */
	warning: function (msg) {
		 $.messager.alert('警告', msg, 'warning');
	},
	/**
	 * 一般提示
	 */
	info: function (msg) {
		 $.messager.alert('提示', msg, 'info');
	},
	/**
	 * 问题提示
	 */
	question: function (msg) {
		 $.messager.alert('问题', msg, 'question');
	},
	/**
	 * 一般无图标提示
	 */
	alert: function (msg) {
		 $.messager.alert('提示', msg);
	},
	/**
	 * 确认弹框
	 */
	confirm: function (msg, callback) {
		$.messager.confirm('提示', msg, function(flag) {
			callback(flag);
        });
	},
	/**
	 * post请求
	 */
	post: function(options) {
		if (!options) {
			APP.error('请传入请求选项，至少包含url');
			return;
		}
		if ($.trim(options.url) == '') {
			APP.error('请传入url选项');
			return;
		}
		var realOptions = {type: 'POST'};
		realOptions.url = options.url;
		if ($.trim(options.dataType) != '') {
			realOptions.dataType = $.trim(options.dataType);
		}
		if ($.trim(options.contentType) != '') {
			realOptions.contentType = $.trim(options.contentType);
		}
		if (options.data != undefined) {
			realOptions.data = options.data;
		}
		if (options.success) {
			realOptions.success = options.success;
		}
		if (options.error) {
			realOptions.error = options.error;
		} else {
			realOptions.error = function (xmlReq, textStatus, errorThrown) {
				APP.message('请求失败：status: ' + xmlReq.status + ', readyState: ' + xmlReq.readyState + ', textStatus: ' + textStatus);
			}
		}
		$.ajax(realOptions);
	},
	/**
	 * get请求
	 */
	get: function (options) {
		if (!options) {
			APP.error('请传入请求选项，至少包含url');
			return;
		}
		if ($.trim(options.url) == '') {
			APP.error('请传入url选项');
			return;
		}
		var realOptions = {type: 'GET'};
		realOptions.url = options.url;
		if ($.trim(options.dataType) != '') {
			realOptions.dataType = $.trim(options.dataType);
		}
		if ($.trim(options.contentType) != '') {
			realOptions.contentType = $.trim(options.contentType);
		}
		if (options.data != undefined) {
			realOptions.data = options.data;
		}
		if (options.success) {
			realOptions.success = options.success;
		}
		if (options.error) {
			realOptions.error = options.error;
		} else {
			realOptions.error = function (xmlReq, textStatus, errorThrown) {
				APP.message('请求失败：status: ' + xmlReq.status + ', readyState: ' + xmlReq.readyState + ', textStatus: ' + textStatus);
			}
		}
		$.ajax(realOptions);
	}
};

/**
 * 公用对象，定义通用方法
 */
var COMMON = {
	/**
	 * html编码
	 */
	htmlEncode: function (str) {
	    var s = '';
	    if (str.length == 0) {
	    	return '';
	    }
	    s = str.replace(/&/g, '>');
	    s = s.replace(/</g, '<');
	    s = s.replace(/>/g, '>');
	    s = s.replace(/ /g, ' ');
	    s = s.replace(/\'/g, "'");
	    s = s.replace(/\"/g, '"');
	    s = s.replace(/\n/g, '<br>');
	    return s;
	},
	/**
	 * html解码
	 */
	htmlDecode: function (str) {
	    var s = '';
	    if (str.length == 0) {
	    	return '';
	    }
	    s = str.replace(/>/g, '&');
	    s = s.replace(/</g, '<');
	    s = s.replace(/>/g, '>');
	    s = s.replace(/ /g, ' ');
	    s = s.replace(/'/g, '\'');
	    s = s.replace(/"/g, '\"');
	    s = s.replace(/<br>/g, '\n');
	    return s;
	},
	/**
	 * html编码,jquery
	 */
	encodeHtml: function (str){
	  return $('<div/>').text(str).html();
	},
	/**
	 * html解码,jquery
	 */
	decodeHtml: function (value){
	  return $('<div/>').html(value).text();
	}
};